package razie.draw.widgets

import razie.Draw
import com.razie.pub.comms._
import razie.base._
import razie.draw._
import java.awt._
import java.awt.event.KeyEvent
import java.awt.event.InputEvent
import razie.draw.Drawable.Widget

/** statics */
object ScriptPad {
      val cmdSCRIPT = new ActionItem("ScriptPad")
}

/** simple scripting pad
 * 
 * @param run - factory, creates the ATI to invoke to run the script. Will receive two args: "language" and "script"
 */
class ScriptPad (val run : () => ActionToInvoke, val simple:Boolean=false, val icontents:String ="<write here>" ) extends Drawable {
//   def mkRun : ActionToInvoke = run()
   var rows = 10
   var cols = 50
   
   override def render (t:Technology, out:DrawStream) : AnyRef = {
      if (Technology.HTMLNOJS == t || simple) {
        val ati = run()
      
        new DrawForm ( 
          ScriptPad.cmdSCRIPT, 
          ati,
          new AttrAccessImpl ("language:String=scala,script:script="+icontents+",sessionId="+ati.getAttr("sessionId"))
          )
      } else if (Technology.HTML == t) { 
        if (out.isInstanceOf[HttpDrawStream]) {
          val s = out.asInstanceOf[HttpDrawStream]
          s.addMeta("<script type=\"text/javascript\" src=\"/public/cm/js/codemirror.js\" charset=\"UTF-8\"></script>")
          s.addMeta("<link rel=\"stylesheet\" type=\"text/css\" href=\"/public/cm/css/docs.css\">")
        } else {
         razie.Log ("WARN-KEYB rendering on unkonwn stream: " + out.getClass.getName)
        }
       
      Draw.seq (
        Draw html "<textarea id=\"code\" cols=\"120\" rows=\"30\">",
        Draw html "function kk () { \"3\" }",
        Draw html "</textarea>",
        Draw html "<script type=\"text/javascript\" src=\"/public/scriptpad.js\" charset=\"UTF-8\"></script>",
        Draw list (
              Draw button (razie.AI("Run Selection"), "javascript:runSelection()"),
              Draw button (razie.AI("Simple style"), "/scripting/simpleSession")
              )
        )
      } else {         "?"
      }
   }
}
